﻿function $(id){return document.getElementById(id);}
function DEditorClass()
{
    this.EditorDesignMode=EditorDesignMode;
    this.OverIco=OverIco;
    this.OutIco=OutIco;
    this.FontBold=FontBold;
    this.FontItalic=FontItalic;
    this.FontUnderLine=FontUnderLine;
    this.RemoveFormats=RemoveFormats;
    this.SetFontSize=SetFontSize;
    this.SetFontName=SetFontName;
    this.SetFontColor=SetFontColor;
    this.SetTextCode=SetTextCode;
    this.InsertLink=InsertLink;
    this.InsertLinkGo=InsertLinkGo;    
    this.InsertUnlink=InsertUnlink;
    this.InsertsImageGo=InsertsImageGo;
    this.InsertsFlashGo=InsertsFlashGo;
    this.InsertSmiley=InsertSmiley;
    this.SendUploadFile=SendUploadFile;
    this.SendUploadImg=SendUploadImg;
    this.SendUploadFlash=SendUploadFlash;
    this.InsertImage=InsertImage;
    this.InsertFlash=InsertFlash;
    this.InsertTypeDataGo=InsertTypeDataGo;
    this.InsertTypeData=InsertTypeData;
    this.IframeToContentText=IframeToContentText;
    this.ContentTextToIframe=ContentTextToIframe;
    this.SaveContentTextGo=SaveContentTextGo;
    this.LoadContentTextGo=LoadContentTextGo;  
    this.HideDiv=HideDiv;
    this.UpFile_OnSubmit=UpFile_OnSubmit;
    this.EditorSize=EditorSize;
    this.OpenWindow=OpenWindow;
    this.EditorDesign=EditorDesign;
    this.EditorPreview=EditorPreview;
    this.EditorHtmlCode=EditorHtmlCode;
    this.InsertsFlashGo=InsertsFlashGo;
    this.SendUploadFlashToPreview=SendUploadFlashToPreview;  
    this.alertUploadFail=alertUploadFail;
    this.deditor=null;
    this.editorID=null;
    this.editorFrame=null;
    this.editor=null;
    this.basePath=null;
    //是否开启设计模式
    function EditorDesignMode(EditorID,BasePath)
    {	
        basePath=BasePath;
        editorID=EditorID;
        editor=$(EditorID);
        editorFrame=$(EditorID+"_iframe");
		deditor =editorFrame.contentWindow;
        deditor.document.designMode="on";
        //deditor.contentEditable="true";
        deditor.document.open();
		deditor.document.writeln("<html><head><title></title><style>.cnblogs_code {background-color:#F5F5F5;border:1px solid #CCCCCC;font-family:Courier New;font-size:13px;overflow:auto;padding:5px;}</style></head><body></body></html>");
        deditor.document.close();
        deditor.document.body.onclick =GetPos;
        deditor.document.body.onselect =GetPos;
        deditor.document.body.onkeyup =GetPos;
        GetPos();
    }
    function GetPos()
    {
        if(isIE())
        {
            deditor.pos = deditor.document.selection.createRange();
        }
    }


    //当鼠标经过工具条图标时
    function OverIco(object)
    {
	    object.className="toolbar_ico_onmouseover";
    }

    //当鼠标离开工具条图标时
    function OutIco(object)
    {
	    object.className="toolbar_ico_onmouseout";
	    object.style.backgroundimage=basePath+"/editor/images/Icobg.gif";
    }
    function exeCommand(cmdName,para1,para2){deditor.focus(); deditor.document.execCommand(cmdName,para1,para2);deditor.focus();}
    function FontBold(){exeCommand('bold',false,null);}//字体加粗
    function FontItalic(){exeCommand('italic',false,null);}
    function FontUnderLine(){exeCommand('underline',false,null);}
    function InsertUnlink(){exeCommand('Unlink',false,null);} //取消链接
    function RemoveFormats(){exeCommand('RemoveFormat',false,null);} //清除格式
    function SetTextCode(){
    if(isIE()){if (deditor.pos.htmlText.length>0){deditor.pos.pasteHTML("<div class='cnblogs_code'>"+deditor.pos.htmlText+"</div>");}}
    else{deditor.document.execCommand('insertHTML',false,"<div class='cnblogs_code'>"+deditor.window.getSelection().toString()+"</div>");}
    }
    function SetFontSize(){var _FontSize = $('dropFontSize').value;exeCommand('FontSize',false,_FontSize);}//设置字体大小
    function SetFontName(){var _FontName = $("dropFontName").value;exeCommand('FontName',false,_FontName);}//设置字体
    function SetFontColor(){var _FontColor = $("dropFontColor").value;exeCommand('ForeColor',false,_FontColor);} //设置字体颜色
    function isIE(){return window.navigator.userAgent.indexOf("MSIE")>=1;}
    function InsertHtml(content,type)
    {
        if(!content){return;}
        //deditor.focus();
        if(isIE())
        {
            switch(type)
            {
                case "image":
                case "flash":
                case "file":
                     deditor.pos.pasteHTML(content);break;
                case "link":
                    if (deditor.pos.htmlText.length>0){ deditor.pos.execCommand('CreateLink',false,content);if (deditor.pos.parentElement().tagName == "A"){deditor.pos.parentElement().target="_blank"; }}break;
               
            }
        }
        else
        {
            switch(type)
            {
                case "link":
                    deditor.document.execCommand('insertHTML',false,"<a href='"+content+"' target='_blank'>"+(deditor.window.getSelection().toString().length>0?deditor.window.getSelection():content)+"</a>");break;
                case "image":
                case "file":
                case "flash":
                    deditor.document.execCommand('insertHTML',false,content);
                    break;
                
            }
        }
        //deditor.focus();
	    HideDiv("PopDiv");
    }
    function InsertFileGo(url){InsertHtml(url,"file");}//插入文件链接至编辑器
    function InsertLinkGo(url){InsertHtml(url,"link");}//插入链接至编辑器
    function InsertsImageGo(url){InsertHtml(url,"image");}  //插入图片至编辑器
    function InsertsFlashGo(content){InsertHtml(content,"flash");} //插入Flash至编辑器

    //插入表情页面代码
    function InsertSmiley()
    {
        var smiley='';
        for (var i = 1; i < 26; i++)
        {
            smiley+="<img src='"+basePath+"/editor/smiley/"+i+".gif' onmouseover=this.className='smiley_ico_onmouseover' onmouseout=this.className='smiley_ico_onmouseout' class='smiley_ico' onclick=deditorClass.SendUploadImg(this.src)>";
        }
        return smiley;
    }
    
    //插入上传的图片至编辑器
    function SendUploadImg(fileUrl)
    {
        InsertsImageGo("<img src='"+fileUrl+"' border='0' align='absmiddle' />");
    }
    function SendUploadFile(fileUrl,title,fileID)
    {
        InsertFileGo("<a target='_blank' href='"+fileUrl+"'><img src='"+basePath+"/Editor/images/file.gif' border='0' align='absmiddle' /> "+title+"</a>(<img  alt='download times' border='0' align='absmiddle' src='"+basePath+"/admin/Editor/Upload.ashx?fid="+fileID+"' />)");
    }
    
    //插入上传的Flash至编辑器
    function SendUploadFlash(fileUrl, width, height)
    {
        if (fileUrl=="" || fileUrl=="http://")
        {
            alert("flash path is empty!");
            return false;
        }
        
        if (!width || !height)
        {
            width=300;
            height=200;
        }
        var strHtml="";
        strHtml+="<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http:\/\/download.macromedia.com\/pub\/shockwave\/cabs\/flash\/swflash.cab#version=7,0,19,0\" width=\""+width+"\" height=\""+height+"\">";
        strHtml+="  <param name=\"movie\" value=\""+fileUrl+"\" \/>";
        strHtml+="  <param name=\"quality\" value=\"high\" \/>";
        strHtml+="  <embed src=\""+fileUrl+"\" quality=\"high\" pluginspage=\"http:\/\/www.macromedia.com\/go\/getflashplayer\" type=\"application\/x-shockwave-flash\" width=\""+width+"\" height=\""+height+"\"><\/embed>";
        strHtml+="<\/object>";
        
        InsertsFlashGo(strHtml);
    }
    
    //插入上传的Flash至预览层
    function SendUploadFlashToPreview(fileUrl, width, height)
    {
        if (fileUrl=="" || fileUrl=="http://")
        {
            return "";
        }
        if (!width || !height)
        {
            width=300;
            height=200;
        }
               
        var strHtml="";
        strHtml+="<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http:\/\/download.macromedia.com\/pub\/shockwave\/cabs\/flash\/swflash.cab#version=7,0,19,0\" width=\""+width+"\" height=\""+height+"\">";
        strHtml+="  <param name=\"movie\" value=\""+fileUrl+"\" \/>";
        strHtml+="  <param name=\"quality\" value=\"high\" \/>";
        strHtml+="  <embed src=\""+fileUrl+"\" quality=\"high\" pluginspage=\"http:\/\/www.macromedia.com\/go\/getflashplayer\" type=\"application\/x-shockwave-flash\" width=\""+width+"\" height=\""+height+"\"><\/embed>";
        strHtml+="<\/object>";
        
        return strHtml;
    }
    
    
    //插入链接
    function InsertLink()
    {
        var strHtml="";
        strHtml+="<div class=\"upload_window\">";
		strHtml+="type:<select name=\"LinkType\" id=\"LinkType\" onchange=\"$(\'txtLinkUrl\').value=this.value\"><option>other<\/option><option value=\"file:\">file:<\/option><option value=\"ftp:\">ftp:<\/option><option value=\"gopher:\">gopher:<\/option><option value=\"http:\" selected=\"selected\">http:<\/option><option value=\"https:\">https:<\/option><option value=\"mailto:\">mailto:<\/option><option value=\"news:\">news:<\/option><option value=\"telnet:\">telnet:<\/option><option value=\"wais:\">wais:<\/option><\/select>";
		strHtml+="<\/div>";
		strHtml+="<div class=\"upload_window\">";
		strHtml+="	url :<input name=\"txtLinkUrl\" id=\"txtLinkUrl\" type=\"text\" value=\"http:\/\/\" size=\"32\">";
		strHtml+="<\/div>";
		strHtml+="<div class=\"upload_window\" style=\"text-align:center;\"><input type=\"button\" name=\"Submit\" value=\"Confirm\" onclick=\"deditorClass.InsertLinkGo($('txtLinkUrl').value)\"><\/div>";
        	
        return strHtml;
    }
    function InsertImage(upfilePath)//插入图片文件页面呈现代码
    {
       var strHtml="";
        strHtml+="<iframe id=\"upfile_iframe\" name=\"upfile_iframe\" width=\"0\" height=\"0\" frameborder=\"0\" scrolling=\"no\"><\/iframe>";
		strHtml+="<form method=\"post\" enctype=\"multipart\/form-data\" name=\"upfile_form\" id=\"upfile_form\" onSubmit=\"return deditorClass.UpFile_OnSubmit(\'"+basePath+"\',\'"+editorID+"_iframe\',\'"+upfilePath+"\');\" target=\"upfile_iframe\">";
		strHtml+="  <div class=\"upload_window\">";
		strHtml+="  	upload:<input type=\"file\" name=\"UpFile\" id=\"UpFile\">&nbsp;<input type=\"submit\" name=\"btnUpFile\" value=\"upload\" id=\"btnUpFile\">";
		strHtml+="  <\/div>";
		strHtml+="<\/form>";
		strHtml+="<div class=\"upload_window\">";
		strHtml+="	remote:<input name=\"txtFileUrl\" id=\"txtFileUrl\" type=\"text\" value=\"http:\/\/\" size=\"38\">";
		strHtml+="<\/div>";
		strHtml+="<div id=\"fileUp_Load\" class=\"upload_window\" style=\"display:none;\"><\/div>";
		strHtml+="<div id=\"filePreview_div\" class=\"upload_window\" style=\"display:none;\">";
		strHtml+="  <div class=\"imgPreview\" id=\"filePreview\"><\/div>";
		strHtml+="<\/div>";
		strHtml+="<div class=\"upload_window\" style=\"text-align:center;\"><input type=\"button\" name=\"Submit\" value=\"Confirm\" onclick=\"deditorClass.SendUploadImg($('txtFileUrl').value)\"><\/div>";
        	
        return strHtml;

    }
    //插入文件页面呈现代码
    function InsertFile(upfilePath)
    {
        var strHtml="";
        strHtml+="<iframe id=\"upfile_iframe\" name=\"upfile_iframe\" width=\"0\" height=\"0\" frameborder=\"0\" scrolling=\"no\"><\/iframe>";
		strHtml+="<form method=\"post\" enctype=\"multipart\/form-data\" name=\"upfile_form\" id=\"upfile_form\" onSubmit=\"return deditorClass.UpFile_OnSubmit(\'"+basePath+"\',\'"+editorID+"_iframe\',\'"+upfilePath+"\');\" target=\"upfile_iframe\">";
		strHtml+="  <div class=\"upload_window\">";
		strHtml+="  	upload:<input type=\"file\" name=\"UpFile\" id=\"UpFile\">&nbsp;<input type=\"submit\" name=\"btnUpFile\" value=\"upload\" id=\"btnUpFile\">";
		strHtml+="  <\/div>";
		strHtml+="<\/form>";
        return strHtml;
    }
    
    
    //插入Flash文件页面呈现代码
    function InsertFlash(upfilePath)
    {
        var strHtml="";
		strHtml+="<form method=\"post\" enctype=\"multipart\/form-data\" name=\"upfile_form\" id=\"upfile_form\" onSubmit=\"return deditorClass.UpFile_OnSubmit(\'"+basePath+"\',\'"+editorID+"_iframe\',\'"+upfilePath+"\');\" target=\"upfile_iframe\">";
		strHtml+="  <div class=\"upload_window\">";
		strHtml+="  	upload:<input type=\"file\" name=\"UpFile\" id=\"UpFile\">&nbsp;<input type=\"submit\" name=\"btnUpFile\" value=\"upload\" id=\"btnUpFile\">";
		strHtml+="  	<iframe id=\"upfile_iframe\" name=\"upfile_iframe\" width=\"0\" height=\"0\" frameborder=\"0\" scrolling=\"no\"><\/iframe>";
		strHtml+="  <\/div>";
		strHtml+="<\/form>";
		strHtml+="<div class=\"upload_window\">";
		strHtml+="	remote:<input name=\"txtFileUrl\" id=\"txtFileUrl\" type=\"text\" value=\"http:\/\/\" size=\"38\">";
		strHtml+="<\/div>";
		strHtml+="<div class=\"upload_window\">";
		strHtml+="	width:<input name=\"txtWidth\" id=\"txtWidth\" type=\"text\" size=\"3\"> px&nbsp;&nbsp;&nbsp;height:<input name=\"txtHeight\" id=\"txtHeight\" type=\"text\" size=\"3\"> px";
		strHtml+="<\/div>";
		strHtml+="<div id=\"fileUp_Load\" class=\"upload_window\" style=\"display:none;\"><\/div>";
		strHtml+="<div id=\"filePreview_div\" class=\"upload_window\" style=\"display:none;\">";
		strHtml+="  <div class=\"imgPreview\" id=\"filePreview\"><\/div>";
		strHtml+="<\/div>";
		strHtml+="<div class=\"upload_window\" style=\"text-align:center;\"><input type=\"button\" name=\"Submit\" value=\"Confirm\" onclick=\"deditorClass.SendUploadFlash($('txtFileUrl').value, $('txtWidth').value, $('txtHeight').value)\"><\/div>";
        return strHtml;
    }

    
    //返回插入内容数据
    function InsertTypeDataGo(InsertType,upfilePath)
    {
        switch (InsertType)
        {
            case "InsertLink":    //插入链接
                return InsertLink();  //返回插入链接页面代码
            case "InsertSmiley":    //插入表情
                return InsertSmiley();  //返回表情数据集合页面代码
            case "InsertImage": //插入图片文件
                return InsertImage(upfilePath);  //返回文件上传窗口页面代码
            case "InsertFlash": //插入Flash
                return InsertFlash(upfilePath);  //返回文件上传窗口页面代码
            case "InsertFile": //插入图片文件
                return InsertFile(upfilePath);  //返回文件上传窗口页面代码
                
        }
    }
    
    //定义初始变量
    var divResult=null;
    var creatFlag=false;
    //弹出层，用与插入相关内容
    function InsertTypeData(object,InsertType,width,upfilePath)
    {       
	    if(creatFlag == true){
	        creatFlag=false;
	        HideDiv("PopDiv");
		    return false;
	    }
	    else{	
		    var etop  = object.offsetTop;
		    var eleft = object.offsetLeft;
		    while (object = object.offsetParent){etop+=object.offsetTop; eleft+=object.offsetLeft;}

		    var divResult = document.createElement("div");
		    divResult.id = "PopDiv";
		    divResult.style.position = "absolute";
		    divResult.style.background = "#DBEFFD";
		    divResult.style.width = width+"px";
		    divResult.style.border = "1px solid #7DACE3";
		    divResult.style.padding = "3px";
		    divResult.style.top = etop+23;
		    divResult.style.left = eleft+1;

            divResult.innerHTML += "<div style='text-align:right;width:100%;'><img src='"+basePath+"/editor/images/div_close.gif' onclick=\"deditorClass.HideDiv('PopDiv')\" style='cursor:pointer;' alt='关闭' /></div>"
            divResult.innerHTML += "<div style='width:100%;'>"+InsertTypeDataGo(InsertType,basePath,upfilePath)+"</div>";
            $('toolbar').appendChild(divResult);
		    
            creatFlag=true;
	    }
    }
    
    function IframeToContentText(){editor.value=deditor.document.body.innerHTML;} //给隐藏域赋值
    function ContentTextToIframe(){deditor.document.body.innerHTML=editor.value;} //给iframe赋值
    
    
  	//载入初始值
    function LoadContentTextGo(){if(isIE()){deditor.document.body.innerHTML=editor.innerText;}else{deditor.document.body.innerHTML=editor.textContent;}}
    
	
	//定时为隐藏域赋值
    function SaveContentTextGo()
    {
        if(!isIE())
        {
            if(editorFrame.style.display=="block" && editor.document.body!=null){IframeToContentText();}window.setTimeout("deditorClass.SaveContentTextGo()",300);
        }
    }
       
    //隐藏层
    function HideDiv(objet)
    {
	    var divResults = $(objet);
	    if (divResults) {
		    var oOpenForm = divResults.parentNode;
		    var oOpenContext = oOpenForm.parentNode;
		    oOpenForm.removeChild(divResults);
		    creatFlag=false;
	    }
    }
	
	//上传文件提交
	function UpFile_OnSubmit(upfilePath)
	{
	    
		if ($('UpFile').value.length == 0 )
		{
			alert( 'file is empth!' ) ;
			return false ;
		}
   
		// 上传提示
		if($('fileUp_Load'))
		{
		    $('fileUp_Load').style.display="block";
		    $('fileUp_Load').innerHTML = 'file is uploading,please wait for a moment...' ;
		}
		//执行Post提交
		document.upfile_form.action=basePath+"/Editor/Upload.ashx?upfilePath="+upfilePath+"&editid="+editorID;


		return true ;
	}
	
	//增大缩小输入框
	function EditorSize(sizeNum, EditorID, Editor_Div)
	{
	    var deditor_div = $(Editor_Div);
	    var deditor_div_h=parseInt(deditor_div.style.height);
	    var deditor_h=parseInt(editorFrame.style.height);
	    var _txtContentText_h=parseInt(editor.style.height);
	    
	    if (deditor_div_h+sizeNum>0 && deditor_h+sizeNum>0)
	    {
	        deditor_div.style.height=deditor_div_h + sizeNum+"px";
	        editorFrame.style.height=deditor_h + sizeNum+"px";
	        editor.style.height=deditor_h + sizeNum+"px";
	    }
	}
	
	//弹出新窗口
	function OpenWindow(url,width,height,type)
	{ 
        var intWidth=width; //窗口宽度
        var intHeight=height;//窗口高度
        var intTop=(window.screen.height-intHeight)/2;
        var intLeft=(window.screen.width-intWidth)/2;
        
        if (type==1)
        {//showModalDialog方式
            window.showModalDialog(url,window,"dialogHeight: "+intHeight+"px; dialogWidth: "+intWidth+"px;dialogTop: "+intTop+"; dialogLeft: "+intLeft+"; resizable: no; status: no;scroll:no");
        }
        else
        {//open方式
            window.open(url,"Detail","Scrollbars=no,Toolbar=no,Location=no,Direction=no,Resizeable=no,Width="+intWidth+" ,Height="+intHeight+",top="+intTop+",left="+intLeft);
        }
    }
	function alertUploadFail()
	{
		alert('upload failed  [make sure file Extension is right and  file size < 4MB!]');
	}
	//返回设计模式
	function EditorDesign()
	{
	    
	    $("toolbar").style.display="block";
	    $("EditorDesign_div").className="toolbar_mode_b";
	    $("EditorPreview_div").className="toolbar_mode_a";
	    $("EditorHtmlCode_div").className="toolbar_mode_a";
	    
	    editor.style.display="none";
	    editorFrame.style.display="block";
	}
	
	//返回预览模式
	function EditorPreview(width, height)
	{
	    HideDiv("PopDiv");
	    //OpenWindow(basePath+"Preview.htm?EditorID="+EditorID,width,height,2);
	    OpenWindow(basePath+"/Editor/Preview.htm?EditorID="+editorID,width,height,2);
	}
	
	//返回HTML源码格式
	function EditorHtmlCode()
	{
	    HideDiv("PopDiv");
	    $("toolbar").style.display="none";
	    $("EditorDesign_div").className="toolbar_mode_a";
	    $("EditorPreview_div").className="toolbar_mode_a";
	    $("EditorHtmlCode_div").className="toolbar_mode_b";
	    
	    editorFrame.style.display="none";
	    editor.style.display="block";
	    IframeToContentText();
	}
}

var deditorClass = new DEditorClass();
